--- Created by tangzebin.
--- DateTime: 2021/10/13 19:55
--- desc 套装转换选择 view
---

---@class UIEquipSwitchSuit:UIBase
UIEquipSwitchSuit = UIBase.New(PanelResNames.UIEquipSwitchSuit)
local MaxCount = 2
local pId
local isAllSame

function UIEquipSwitchSuit.Init(paId,isAllSameSuit)
    pId = paId
    isAllSame = isAllSameSuit
    UIBase.Init(UIEquipSwitchSuit)
end

function UIEquipSwitchSuit:OnCreate()
    self.btnClose = self:GetChild("bgClose")
    self.giftList = self:SetDynamicList(self:GetChild("list"),self.updateItem)
    self:AddUIEvent()
    self.giftList.numItems = MaxCount
    self.cacheMap = {}
end

function UIEquipSwitchSuit:AddUIEvent()
    self:RegisterOnClick(self.btnClose,self.OnClickClose)
end

function UIEquipSwitchSuit:OnClickClose()
    closeUI(PanelNames.UIEquipSwitchSuit)
end

function UIEquipSwitchSuit:Refresh()
end

---@class UISwitchSuit
---@param ntext
---@param btnGet
---@param icon
---@return UISwitchSuit
function UIEquipSwitchSuit:BuildItem(itemObj)
    return
    {
        ntext = itemObj:GetChild("packageName"),
        btnGet = itemObj:GetChild("n3");
        icon = itemObj:GetChild("icon");
    }
end

function UIEquipSwitchSuit:updateItem(index, itemObj) --todo
    local showTxt = getLanguage("EquipSuit"..(index))
    local key = itemObj.id
    self.cacheMap = self.cacheMap or {}
    local item = self.cacheMap[key]
    if not item then
        item = self:BuildItem(itemObj)
        self.cacheMap[key] = item
    end

    item.icon.url = EnumConst.AtlasPath.ICONAtlas.."suit"..index
    item.btnGet.onClick:Clear();
    item.btnGet.onClick:Add(function()
        if self.last_click_switch_suit ~= nil and UnityEngineTime.realtimeSinceStartup - self.last_click_switch_suit < 5 then
            UISysTips.AddMsg(getLanguage("EquipSwitchTips1"))
            return
        end
        self.last_click_switch_suit = logicMgr.EquipManager.getLastSendSwitchTime()
        equipHandler.sendSwitchSuit(pId,index,-1)
        closeUI(PanelNames.UIEquipSwitchSuit)
    end)
    item.ntext.text = showTxt
end

function UIEquipSwitchSuit:OnDestroy()
    self.cacheMap = nil
end

return UIEquipSwitchSuit